<?php
/**
 * CModelConfig class file.
 * 获取或设置控制器配置数据.
 * @author Huan Song <iphper@yeah.net>
 * @version $Id: CModelConfig.php 1 2011-01-16 09:16:06Z huan.song $
 * @package system.model
 * @since 2.0
 */
class CModelConfig extends CConfig
{	
	/**
	 * @var string Model类后缀名.
	 */
	protected static $_modelKey;
	
	/**
	 * @var string Model目录.
	 */
	protected static $_modelDir;
	
	/**
	 * 获取Model类后缀名.
	 * <pre>
	 * $modelKey = CModelConfig::getModelKey();
	 * </pre>
	 * @return string Model类后缀名.
	 * @see setModelKey
	 */
	public static function getModelKey()
	{
		if (null === self::$_modelKey)
			self::setModelKey();
		
		return self::$_modelKey;
	}
	
	/**
	 * 设置Model类后缀名.
	 * <pre>
	 * $modelKey = null;
	 * CModelConfig::setModelKey($modelKey);
	 * 如果$modelKey === null, 取配置中
	 * 'model' => array(
	 * 		'modelKey' => 'Model', //Model类后缀名.
	 * 	),
	 * 结果: self::$_modelKey = 'Model'.
	 * </pre>
	 * @param string|null $modelKey. 如果为null, 取用户自定义的配置路径; 如果配置中没定义, 默认为'Model'.
	 * @see getModelKey
	 */
	public static function setModelKey($modelKey = null)
	{
		if (null === $modelKey)
		{
			$modelKey = self::get('model', 'modelKey');
			if (null === $modelKey)
				$modelKey = 'Model';
		}
		
		self::$_modelKey = $modelKey;
	}
	
	/**
	 * 获取Model目录.
	 * <pre>
	 * $modelDir = CModelConfig::getModelDir();
	 * </pre>
	 * @return string Model目录.
	 * @see setModelDir
	 */
	public static function getModelDir()
	{
		if (null === self::$_modelDir)
			self::setModelDir();
		
		return self::$_modelDir;
	}
	
	/**
	 * 设置Model目录.
	 * <pre>
	 * $modelDir = null;
	 * CModelConfig::setModelDir($modelDir);
	 * 如果$modelDir === null, 取配置中
	 * 'model' => array(
	 * 		'modelDir' => APP_DIR . '/models' //Model目录.
	 * 	),
	 * 结果: self::$_modelDir = APP_DIR . '/models'.
	 * </pre>
	 * @param string|null $modelDir. 如果为null, 取用户自定义的配置路径; 如果配置中没定义, 默认为'app/models'.
	 * @throws CModelException. 如果Model目录不存在, 抛出异常.
	 * @see getModelDir
	 */
	public static function setModelDir($modelDir = null)
	{
		if (null === $modelDir)
		{
			$modelDir = self::get('model', 'modelDir');
			if (null === $modelDir)
				$modelDir = APP_DIR . DS . 'models';
		}
		
		if (!is_dir($modelDir))
			throw new CModelException('CModelConfig:setModelDir modelDir not exists', array('{modelDir}' => $modelDir));
		
		self::$_modelDir = $modelDir;
	}
}